const getDistance = function(lat1, lng1, lat2, lng2) {
	let rad = Math.PI / 180.0;
	var radLat1 = lat1 * rad;
	var radLat2 = lat2 * rad;
	var a = radLat1 - radLat2;
	var b = lng1 * rad - lng2 * rad;
	var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
		Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
	s = s * 6378.137; // EARTH_RADIUS;
	s = Math.round(s * 10000) / 10000; //输出为公里
	s = s.toFixed(2); //保留2位小数
	return s;
}
const  bMapTransQQMap = function(lng,lat){
        let x_pi = 3.14159265358979324 * 3000.0 / 180.0;
        let x = lng - 0.0065;
        let y = lat - 0.006;
        let z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
        let theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
        let lngs = z * Math.cos(theta);
        let lats = z * Math.sin(theta);
        return {
            lng: lngs,
            lat: lats
        }
}


//计算矩形范围，最长角是不对的不过能满足基本需求 半径的单位是M
const getRectangle = function(lat, lng, radius) {
	let deg = 2 * Math.PI * 6378137 / 360.0; //地球周长 每一度的距离 2πR
	let dpmLat = 1 / deg;
	let radiusLat = dpmLat * radius;
	let minLat = lat - radiusLat;
	let maxLat = lat + radiusLat;
	let mpdLng = deg * Math.cos(lat * (3.141592653 / 180))
	let dpmLng = 1 / mpdLng;
	let radiusLng = dpmLng * radius;

	let minLng = lng - radiusLng; // 最小经度
	let maxLng = lng + radiusLng; // 最大经度

	return {
		minLat: minLat,
		maxLat: maxLat,
		minLng: minLng,
		maxLng: maxLng
	};
}


const djsFull = function(t) {
	let date1 = new Date(t);
	let date2 = new Date(date1);
	// let y = date2.getFullYear();
	// let m = date2.getMonth() + 1;
	// var d = Math.floor(t / 86400);
	// var h = Math.floor(t % 86400 / 3600);
	// // var m = Math.floor(t % 3600 / 60);
	// var s = Math.floor(t % 60);
	
	let y = date2.getFullYear();
	let m = date2.getMonth() + 1;
	let d = date2.getDate();
	let h = date2.getHours();
	let i = date2.getMinutes();
	let s = date2.getSeconds();

	return {
		y,
		d: d,
		h: this.addzero(h),
		m: this.addzero(m),
		s: this.addzero(s),
		i:this.addzero(i)
	}
}

//m {y:2020,m:8,month:'08'}
const upMonth = function(m){
	let _m = parseInt(m.m) - 1;
	let _y = parseInt(m.y);
	if(_m == 0){
		_y = _y - 1;
		_m = 12;
	}
	return {
		y:_y,
		m:this.addzero(_m)
	}
}
const downMonth = function(m){
	let _m = parseInt(m.m) + 1;
	let _y = parseInt(m.y);
	if(_m == 13){
		_y = _y+1;
		_m = 1;
	}
	return {
		y:_y,
		m:this.addzero(_m)
	}
}
//获取一个月的日期 month 代表是 下几个月 还是上几个月
function getNowMonth(week) {
	// console.log('weekweekweek',week);
	let date = new Date();
	let year = date.getFullYear();
	let month = date.getMonth() + 1;
	let d = new Date(year, month, 0).getDate();
	let day = date.getDate();
	month = month > 9 ? month : "0" + month;
	let a = this.getTimeArr(2);
	let arr = []
	let state = week?week:0;
	for (let index = state;index < state+30;index++) {
		arr.push(this.getTimeArr(index));
	}
	// console.log('arrrrrr',arr);
	return arr;
}
const getMonthArr = function(week){

	let today = this.getTimeArr();
	let w = today.w;
	week = week ? week : 0;
	let bgDay = week * 7 - w;//星期开头的天
	let endDay =week * 7 - w + 6;//本周的最后一天  
	let arr = [];
	for(var a = bgDay;a<= endDay;a++){
	   arr.push(this.getTimeArr(a));
	}
	return arr;
}
//获取一个星期的日期 week 代表是 下几个星期 还是上几个星期
const getWeekArr = function(week){
	let today = this.getTimeArr();
	let w = today.w;
	week = week ? week : 0;
	let bgDay = week * 7 - (w-1);//星期开头的天
	let endDay =week * 7 - w + 7;//本周的最后一天  
	let arr = [];
	for(var a = bgDay;a<= endDay;a++){
	   arr.push(this.getTimeArr(a));
	}
	return arr;
}
//获取一个星期的日期 week 代表是 下几个星期 还是上几个星期 周一开头
const getMondayArr = function (week) {
	let today = this.getTimeArr();
	let w = today.w;
	week = week ? week : 0;
	let bgDay;
	let endDay;
	if (w === 0) {
		 bgDay = (week * 7) - 6;//星期开头的天
		 endDay = (week * 7);//本周的最后一天  
	}else {
		 bgDay = week * 7 - w + 1;//星期开头的天
		 endDay = week * 7 - w + 7;//本周的最后一天 
	}
	 
	let arr = [];
	for (var a = bgDay; a <= endDay; a++) {
		arr.push(this.getTimeArr(a));
	}
	return arr;
}


const getTimeArr = function(day) {
	let date1 = new Date();
	let date2 = new Date(date1);
	day = day ? day : 0;
	date2.setDate(date1.getDate() + day);
	let y = date2.getFullYear();
	let m = date2.getMonth() + 1;
	let d = date2.getDate();
	let h = date2.getHours();
	let i = date2.getMinutes();
	let s = date2.getSeconds();
	
	let dt = '上午';
	if(h >=9 && h<=11){
		dt = '上午';
	}else if(h <9 && h>5){
		dt = '早上';
	}else if(h>11 && h<14){
		dt = '中午';
	}else if(h>=14 && h<18){
		dt = '下午';
	}else{
		dt = '晚上';
	}
	let w = date2.getDay();
	let week = ['日','一','二','----------------------------------------------------------------------------------------------------------------------------411111111111111111111111111111111111111三','四','五','六'];
	let en_week = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
	let getMonth = ['一','二','三','四','五','六','七','八','九','十','十一','十二']
	return {
		y: y,
		m: this.addzero(m),
		d: this.addzero(d),
		h: this.addzero(h),
		i: this.addzero(i),
		s: this.addzero(s),
		w: w,
		md:this.addzero(m)+'/'+this.addzero(d),
		wd:week[w],
		ed:en_week[w],
		t: dt,
		mh:getMonth[m-1],
		today:day ? 0 : 1,
		time:date2.getTime()
	}
}

const datetimeToUnix = function(str) {
	var _arr = str.split(' ');
	var _day = _arr[0].split('-');
	_arr[1] = (_arr[1] == null) ? '0:0:0' : _arr[1];
	var _time = _arr[1].split(':');
	for (var i = _day.length - 1; i >= 0; i--) {
		_day[i] = isNaN(parseInt(_day[i])) ? 0 : parseInt(_day[i]);
	}
	for (var i = _time.length - 1; i >= 0; i--) {
		_time[i] = isNaN(parseInt(_time[i])) ? 0 : parseInt(_time[i]);
	}
	_time[2] = 0;
	var _temp = new Date(_day[0], _day[1] - 1, _day[2], _time[0], _time[1], _time[2]);
	return _temp.getTime() / 1000;
}


const unixToDatetime = function(format, time) {
	var _temp = (time != null) ? new Date(time * 1000) : new Date();
	var _return = '';

	if (/Y-m-d/.test(format)) {
		var _day = [_temp.getFullYear(), this.addzero(1 + _temp.getMonth()), this.addzero(_temp.getDate())];
		_return = _day.join('-');
	}
	if (/H:i:s/.test(format)) {
		var _time = [this.addzero(_temp.getHours()), this.addzero(_temp.getMinutes()), this.addzero(_temp.getSeconds())];
		_return += ' ' + _time.join(':');
	}
	return _return;

}

const addzero = function(i) {
	if (i <= 9) {
		return '0' + i;
	} else {
		return i;
	}
}

export default {
	getDistance,
	getRectangle,
	djsFull,
	getTimeArr,
	getWeekArr,
	datetimeToUnix,
	unixToDatetime,
	addzero,
	upMonth,
	downMonth,
	bMapTransQQMap,
	getNowMonth,
	getMonthArr,
	getMondayArr
}
